<?php 
class DomArTestUnitTestCase extends UnitTestCase {
	
	public function setUp() {

    	DomSql::init( 'localhost', 'root', '');
		$this->assertTrue( DomSql::connected() );
		
		try {DomSql::query('CREATE DATABASE `DomArTestDatabase`');} catch( RuntimeException $e ) {}
		try {DomSql::query('CREATE DATABASE `DomArTestDatabase2`');} catch( RuntimeException $e ) {}
		
		$this->assertTrue( DomSql::selectDatabase( 'DomArTestDatabase' ) );

    }
    
    public function tearDown() {
	
		// remove all indexes and foreign keys
		foreach( DomSqlTable::getDatabaseTables('DomArTestDatabase') as $table  ) {
			if( $table->exists() ) {
				foreach( $table->getIndexes() as $index ) {
					try {
						$table->dropForeignKey( $index['name']);
					} catch( RuntimeException $e ) { 
						//echo exceptionToString ($e);	
					}
					try {
						$table->dropIndex( $index['name']);
					} catch( RuntimeException $e ) { 
						//echo exceptionToString ($e);	
					}
				}
			}
		}
		
		foreach( DomSqlTable::getDatabaseTables('DomArTestDatabase2') as $table  ) {
			if( $table->exists() ) {
				try {
					$table->dropForeignKey( $index['name']);
				} catch( RuntimeException $e ) { 
					//echo exceptionToString ($e);	
				}
				try {
					$table->dropIndex( $index['name']);
				} catch( RuntimeException $e ) { 
					//echo exceptionToString ($e);	
				}
			}
		}	
	
	
		DomSql::query('DROP DATABASE `DomArTestDatabase2`');
		DomSql::query('DROP DATABASE `DomArTestDatabase`');
					
	/*	try {
			DomSql::query('DROP DATABASE `DomArTestDatabase`');
		} catch( RuntimeException $e ) { 
			if($e->getCode() != 1008) {
				try {
					DomSql::query('DROP DATABASE `DomArTestDatabase2`');
					DomSql::query('DROP DATABASE `DomArTestDatabase`');
				} catch( RuntimeException $e ) { 
					if($e->getCode() != 1008) {
						throw $e;	
					}
				}
			} 
		}*/
    }
	
	final public function assertDatabaseValue( $table, $field, $id, $expectedValue, $message = NULL ) {
		$r = DomSql::selectOneQuery('SELECT `'.$field.'` FROM '.$table.' WHERE id = '.$id, $field );
		if( $message == NULL ) {
			$message = 'Excpected ['.substr($expectedValue,0,40).'] but found ['.substr($r,0,40).'] from database!';
		}
		
		return $this->assert( new TrueExpectation(), $r == $expectedValue, $message);
	}
	
	final public function assertDatabaseNull( $table, $field, $id, $message = NULL ) {
		$r = DomSql::selectOneQuery('SELECT id, `'.$field.'` FROM '.$table.' WHERE id = '.$id.' AND `'.$field.'` <=> NULL LIMIT 1' );
		if( $message == NULL ) {
			$message = 'Excpected [NULL] but found ['.substr($r[$field],0,40).'] from database!';
		}
		return $this->assert( new TrueExpectation(), (bool)$r['id'], $message);
	}
	
	final public function assertObjectDatabaseValue( DomArObject $object, $property, $expectedValue, $message = NULL ) {
		if( !is_object($object) ){
			$this->assertTrue(false,'Object not given to assertObjectDatabaseValue ');
			return;
		}
		$this->assertDatabaseValue( $object->getDbTable(), $property, $object->getId(), $expectedValue, $message );
	}
	
	final public function assertObjectExist( DomArObject $object, $message = NULL  ) {
		if( !is_object($object) ){
			$this->assertTrue(false,'Object not given to assertObjectExist ');
			return;
		}
		$r = DomSql::selectOneQuery('SELECT id FROM '.$object->getDbTable().' WHERE id = '.$object->id, 'id' );
		return $this->assert( new TrueExpectation(), $r > 0, $message);
	}
	
	final public function assertObjectNotExist( DomArObject $object, $message = NULL  ) {
		$r = DomSql::selectOneQuery('SELECT id FROM '.$object->getDbTable().' WHERE id = '.$object->id, 'id' );
		return $this->assert( new FalseExpectation(), $r > 0, $message);
	}
	
	final public function assertTableEmpty( $tableName, $message = NULL ) {
		try {
			$r = DomSql::selectOneQuery('SELECT count(*) as n FROM '.addslashes($tableName).'','n');
		} catch( Exception $e ) {
			$r = 0;
		}
		return $this->assert( new TrueExpectation(), $r == 0, $message);
	}
	
}
?>